<?php
//PHP API CONSUMER HELPER
function rest_helper($url, $params = null, $verb = 'POST', $format = 'xml')
{
  $cparams = array(
    'http' => array(
      'method' => $verb,
      'ignore_errors' => true
    )
  );
  if ($params !== null) {
    $params = http_build_query($params);
    if ($verb == 'POST') {
      $cparams['http']['content'] = $params;
    } else {
      $url .= '?' . $params;
    }
  }

  $context = stream_context_create($cparams);
  $fp = fopen($url, 'rb', false, $context);
  if (!$fp) {
    $res = false;
  } else {
    // If you're trying to troubleshoot problems, try uncommenting the
    // next two lines; it will show you the HTTP response headers across
    // all the redirects:
    // $meta = stream_get_meta_data($fp);
    // var_dump($meta['wrapper_data']);
    $res = stream_get_contents($fp);
  }

  if ($res === false) {
    throw new Exception("$verb $url failed: $php_errormsg");
  }

  switch ($format) {
    case 'json':
      $r = json_decode($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as json");
      }
      return $r;

    case 'xml':
      $r = simplexml_load_string($res);
      if ($r === null) {
        throw new Exception("failed to decode $res as xml");
      }
      return $r;
  }
  return $res;
}

$secret = $_POST['secret'];
$pubid = $_POST['pubid'];
$apikey = $_POST['apikey'];
$ks = $_POST['ks'];
$x7server = $_POST['x7server'];
$name = $_POST['name'];
$user_login = $_POST['user_login'];
$x7jcname = $_POST['x7jcname'];
$x7kalpartnerid = $_POST['x7kalpartnerid'];
$x7bloghome = $_POST['x7bloghome'];

$url = "http://$x7jcname.api.jaycut.com/mixes/$pubid/exports";

if ( eregi ( "$x7bloghome", $_SERVER['HTTP_REFERER'] ) )
{
/* Request data */
$request_path = "/mixes/$pubid/exports";
$http_method = 'GET';
$signature = sha1($secret . $http_method . $request_path);

/* Create a cURL session with options. */
$ch = curl_init();
$ch_options = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_VERBOSE => TRUE,
CURLOPT_USERPWD => $apikey . ':' . $signature,
CURLOPT_URL =>
'http://' . $x7jcname . '.api.jaycut.com' . $request_path
);
curl_setopt_array($ch, $ch_options);

/* Perform the request */
$r_body = curl_exec($ch);
$r_info = curl_getinfo($ch);

/* Extract the uri to the exported file */
$XML = simplexml_load_string($r_body);
foreach ($XML->export->params->param as $param)
{
   switch((string) $param['name']) { // Get attributes as element indices
   case 'url':
       $param = str_replace("true", "false", "$param");
       $file_url = $param;
       break;
   case 'email':
       $email = $param;
       break;
   }
}
curl_close($ch);

$xmlresult = rest_helper("$x7server/api_v3/?service=media&action=addfromurl",
					array(
                                            'ks' => "$ks",
                                            'url' => "$file_url",
                                            'mediaEntry:name' => "$name",
                                            'mediaEntry:mediaType' => '1',
                                            'mediaEntry:type' => '1',
                                            'mediaEntry:userId' => "$user_login",
                                            'mediaEntry:partnerId' => "$x7kalpartnerid"
                                            ), 'POST'
					 );
} else { //failed security check
  exit;
}
?>